package com.bnlr.lrparking.appui.bank;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.event.AddBankCardSucceeEvent;
import com.bnlr.lrparking.model.VerifyBankBean;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.view.ClearEditText;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

/**
 * @author hp
 * @date 2017/8/31
 * 添加银行卡
 */
public class AddBankCardOneActivity extends BaseActivity {

    private TextView tvNavTitle;
    private TextView ed_cardholder;
    private EditText ed_cardholdernumber;
    private ClearEditText ed_bankaddress;
    private TextView tv_addbankcard_btn;
    private ImageView ivClear;
    private String strbankaddress="";
    private String realName;
    private String s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addbankcard);
        realName = getIntent().getStringExtra("real_name");
        initView();
    }

    private void initView(){
        EventBus.getDefault().register(this);
        tvNavTitle=(TextView)findViewById(R.id.tv_nav_title);
        tvNavTitle.setText("添加银行卡");
        ed_cardholder=(TextView)findViewById(R.id.ed_cardholder);
        ed_cardholdernumber=(EditText) findViewById(R.id.ed_cardholdernumber);
        ed_bankaddress=(ClearEditText)findViewById(R.id.ed_bankaddress);
        tv_addbankcard_btn=(TextView)findViewById(R.id.tv_addbankcard_btn);
        ivClear = (ImageView) findViewById(R.id.clear_bank_num);
        tv_addbankcard_btn.setEnabled(false);
        ed_cardholder.setText(realName);
        initListener();
        ivClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ed_cardholdernumber.setText("");
            }
        });
    }

    private void initListener() {
        ed_cardholdernumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())){
                    ivClear.setVisibility(View.VISIBLE);
                } else {
                    ivClear.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(s.toString()) && !TextUtils.isEmpty(ed_bankaddress.getText().toString()) &&
                        s.toString().replace(" ","").length() >= 13) {
                        tv_addbankcard_btn.setEnabled(true);
                        tv_addbankcard_btn.setBackgroundResource(R.drawable.seletor_login_btn);
                } else {
                    tv_addbankcard_btn.setEnabled(false);
                    tv_addbankcard_btn.setBackgroundResource(R.mipmap.login_disabled);
                }
            }
        });


        ed_bankaddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString()) && !TextUtils.isEmpty(ed_bankaddress.getText().toString())
                        && ed_cardholdernumber.getText().toString().length() >= 13) {
                    strbankaddress=ed_bankaddress.getText().toString();
                    tv_addbankcard_btn.setEnabled(true);
                    tv_addbankcard_btn.setBackgroundResource(R.drawable.seletor_login_btn);
                } else {
                    tv_addbankcard_btn.setEnabled(false);
                    tv_addbankcard_btn.setBackgroundResource(R.mipmap.login_disabled);
                }
            }
        });

        ed_cardholdernumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
               if (hasFocus && !TextUtils.isEmpty(ed_cardholdernumber.getText().toString())) {
                   ivClear.setVisibility(View.VISIBLE);
               } else {
                   ivClear.setVisibility(View.GONE);
               }
            }
        });
        tv_addbankcard_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              verifyBankType();
            }
        });
    }

    /**
     * 验证银行卡
     */
    private void verifyBankType() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "GetBankCardType");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("cardNo",ed_cardholdernumber.getText().toString().replace(" ",""));
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<VerifyBankBean>(baseContext, true, VerifyBankBean.class) {
                        @Override
                        public void doWork(VerifyBankBean data, String code) {
                            if (code.equals("1")) {
                                Intent intent=new Intent(AddBankCardOneActivity.this,AddBankCardTwoActivity.class);
                                if (data.getData().getCarType().equals("0")) {
                                    intent.putExtra("bankType",data.getData().getBankName()+"  借记卡");
                                } else {
                                    intent.putExtra("bankType",data.getData().getBankName()+"  信用卡");
                                }
                                intent.putExtra("bankNo",ed_cardholdernumber.getText().toString().replace(" ",""));
                                intent.putExtra("bankUser",ed_cardholder.getText().toString());
                                intent.putExtra("bankaddress",strbankaddress);
                                startActivity(intent);
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (!code.equals("1")) {
                                    ToastUtils.showShort(obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    }, true);
        } catch (Exception e) {

        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBankCardSucceeEvent(AddBankCardSucceeEvent event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
